<?php

namespace backend\modules\supplier\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\supplier\models\storage;

/**
 * storageSearch represents the model behind the search form about `backend\modules\supplier\models\storage`.
 */
class storageSearch extends storage {

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['id', 'supplier_id'], 'integer'],
            [['name', 'address', 'linkman', 'tel', 'remark'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios() {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params) {
        $params['storageSearch']['supplier_id'] = Yii::$app->user->identity->supplier_id;

        $query = storage::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        if (!($this->load($params) && $this->validate())) {
            return $dataProvider;
        }

        $query->andFilterWhere([
            'id' => $this->id,
            'supplier_id' => $this->supplier_id,
        ]);

        $query->andFilterWhere(['like', 'name', $this->name])
                ->andFilterWhere(['like', 'address', $this->address])
                ->andFilterWhere(['like', 'linkman', $this->linkman])
                ->andFilterWhere(['like', 'tel', $this->tel])
                ->andFilterWhere(['like', 'remark', $this->remark]);

        return $dataProvider;
    }

}
